<?php

/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: 2/25/2012
 * @project		: CodeGenerater
 * @description	:
 *		- Contain form edit, add
 *
\* ************************************************** */



    /* ------------------------------------------------------------ *\
    | - FUNC Purpose	: create radio, checkbox, combobox, listbox 
    | - Parameters		: 
    |		Name			 Type		Description
    |		---------------------------------------------------
    |		$field           array      control infor 	
    |       $field_id        string						
    |	    $required        bool       
    |	    $label           string     
    |	    $option          string     
    | - Return
    |			- Control
    \* ------------------------------------------------------------ */
    function create_form_add( $field, $field_id = '', $label = '', $required = false,  $option = '' ){
        if( !isset($field['control'])){
            //print_r($field);
            //return get_FormInput(); $field_id, $label, $required, $option 
            return create_textfield( $field_id, $label, $required, $option );
        }else{
            switch( $field['control']['control_type'] ){
                case "RB":  // Radio control
                    // Call func create radio control
                    return create_radio($field, '', $required, $label);

                case "CB":  // Checkbox control
                    // Call func create checkbox control
                    return create_checkbox($field, '', $required, $label);
                case "CMB": // ComboBox
                    // Call func create combobox control
                    return create_combobox($field, $required, $label );
                case "LB":  // ListBox
                    // Call func create listbox control
                    return create_listbox($field, $required, $label);
            }
        }
    }
    
    
    function create_radio( $radio_data, $style = 'ctl', $required = false, $label = '' ){
        $r = '';
        if($required){
            $r = '<span class="required"><font color="red">*</font></span>';
        }
        $result = '<span style="clear:both;float:left;padding:5px; margin-left:130px;">';
        $result = $result . "<fieldset><legend>" . $radio_data['field_id'] . $r ."</legend>" ;
        if($style == 'ltc'){
            foreach($radio_data['RB'] as $data){
                $result = $result . "<label style='cursor:pointer;width:50px;line-height:15px;padding-left:5px;margin:5px;float:left;'". 
                "for = '" . $data['id'].$radio_data['field_name'] . "'>" . urldecode($data['value']) . "</label>" ;
                if($data['default'] == 'true'){
                    $result = $result . "<input type = 'radio' id = '" . $data['id'].$radio_data['field_name'] . 
                        "'name='" . $radio_data['field_name'] .
                        "' value = '" . urldecode($data['value']) . "' checked = 'true' style='cursor:pointer;margin:6px' />";
                }else{
                    $result = $result . "<input type = 'radio' id = '" . $data['id'].$radio_data['field_name'] . 
                        "'name='" . $radio_data['field_name'] . "' value = '" . urldecode($data['value']) . "' style='cursor:pointer;margin:6px' />";
                }
                $result = $result . "<br />";
            }
            $result = $result . "</fieldset>";
        }else{
            foreach($radio_data['control']['control_value'] as $data){
                if($data['default'] == 'true'){
                    $result = $result . "<input type = 'radio' id = '" . $data['id'] . "' name = '" . $radio_data['field_id'] .
                        "' value = '" . urldecode($data['value']) . "' checked = 'true' style='cursor:pointer' />";
                }else{
                    $result = $result . "<input type = 'radio' id = '" . $data['id'] . "' name = '" . $radio_data['field_id'] . 
                        "' value = '" . urldecode($data['value']) . "' style='cursor:pointer' />";
                }
                $result = $result . "<label style='cursor:pointer' for = '" . $data['id'] . "'>" . urldecode($data['value']) . "</label>" ;
                $result = $result . "<br />";
            }
            $result = $result . "</fieldset>";
        } 
        $result = $result . "</span>";
        return $result;
    }
    
    
    function create_checkbox( $checkbox_data, $style = 'ctl', $required = false, $label = '' ){
        $r = '';
        if($required){
            $r = '<span class="required"><font color="red">*</font></span>';
        }
        $result = '<span style="clear:both;float:left;padding:5px; margin-left:130px;">';
        $result = $result . "<fieldset><legend>" . $checkbox_data['field_id'] . $r ."</legend>" ;
        if($style == 'ltc'){
            foreach($checkbox_data['control']['control_value'] as $data){
                $result = $result . "<label style='cursor:pointer' for = '" . $data['id'].$checkbox_data['field_id'] . "'>" . urldecode($data['value']) . "</label>" ;
                if($data['default'] == 'true'){
                    $result = $result .  "<input type = 'checkbox' id = '" . $data['id'].$checkbox_data['field_id'] . 
                        "'name='" . $checkbox_data['field_id'] . 
                        "' value = '" . urldecode($data['value']) . "'  checked = 'true' style='cursor:pointer' />";
                }else{
                    $result = $result . "<input type = 'checkbox' id = '" . $data['id'].$checkbox_data['field_id'] . 
                        "'name='" . $checkbox_data['field_id'] . "' value = '" . urldecode($data['value']) . "'  style='cursor:pointer' />";
                }
                $result = $result .  "<br />";
            } 
            $result = $result . "</fieldset>";
        }else{
            foreach($checkbox_data['control']['control_value'] as $data){
                if($data['default'] == 'true'){
                    $result = $result . "<input type = 'checkbox' id = '" . $data['id'].$checkbox_data['field_id'] . 
                        "' name='" . $checkbox_data['field_id'] . 
                        "' value = '" . urldecode($data['value']) . "'  checked = 'true' style='cursor:pointer' />";
                }else{
                    $result = $result . "<input type = 'checkbox' id = '" . $data['id'].$checkbox_data['field_id'] . 
                        "'name='" . $checkbox_data['field_id'] . "' value = '" . urldecode($data['value']) . "' style='cursor:pointer' />";
                }
                $result = $result . "<label style='cursor:pointer' for = '" . $data['id'].$checkbox_data['field_id'] . "'>" . urldecode($data['value']) . "</label>" ;
                $result = $result . "<br />";
            }
             $result = $result . "</fieldset>";
        }
        $result = $result . "</span>";
        return $result;
    }    

    function create_combobox( $combobox_data, $required = false, $label = ''){
        $r = '';
        if($required){
            $r = '<span class="required"><font color="red">*</font></span>';
        }
        $result = '<span style="clear:both;float:left;padding:5px; margin-left:130px;">';
        $result = $result . '<label>' . $label. $r. '</label>';
        $result = $result . '<select size="1" style="width: 100px" name= '. $combobox_data['field_id'] .' >';
        foreach($combobox_data['control']['control_value'] as $data){
            if($data['default'] == 'true'){
                $result = $result . "<option id = '" . $data['id'] . "' value = '" . urldecode($data['value']) . 
                    "' selected = 'true'>" . urldecode($data['value']) . "</option>";
            }else{
                $result = $result . "<option id = '" . urldecode($data['id']) . "' value = '" . urldecode($data['value']) . "'>" . 
                urldecode($data['value']) . "</option>";
            }
        }
        $result = $result . '</select>';
        $result = $result . '</span>';
        return $result;
    } 
    
    function create_listbox( $listbox_data, $required = false, $label = ''){
        $r = '';
        if($required){
            $r = '<span class="required"><font color="red">*</font></span>';
        }
        $result = '<span style="clear:both;float:left;padding:5px; margin-left:130px;">';
        $result = $result . '<label>' . $label. $r. '</label>';
        $result = $result . '<select size="5" style="width:100px" name= "'. $listbox_data['field_id'] .'" >';
        foreach($listbox_data['control']['control_value'] as $data){
            if($data['default'] == 'true'){
                $result = $result . "<option id = '" . $data['id'] . "' value = '" . urldecode($data['value']) . 
                    "' selected = 'true'>" . urldecode($data['value']) . "</option>";
            }else{
                $result = $result . "<option id = '" . $data['id'] . "' value = '" . urldecode($data['value']) . "'>" . 
                urldecode($data['value']) . "</option>";
            }
        }
        $result = $result . '</select>';
        $result = $result . '</span>';
        return $result;
    }    
   /* 
    function create_textfield( $label = '', $required = false, $value = ''){
        $r = '';
        if($required){
            $r = '<span class="required"><font color="red">*</font></span>';
        }
        $result = '<span style="clear:both;float:left;padding:5px; margin-left:130px;">';
        $result = $result . '<label>' . $label . $r . '</label>';
        $result = $result . '<input type = "text" id = "' . $value . '" name = "' . 
                $value . '" value = ""/>'; 
        $result = $result . '</span>';
        return $result;
    }
    */
    
    function create_textfield( $field_id, $label, $required, $option ){
        $r = '';
        if($required){
            $r = '<span class="required"><font color="red">*</font></span>';
        }        
        $add_edit ='';
        if( $option == 'add' ){
            $add_edit = 'set_value(\'' . $field_id . '\');';
        }else{
            $add_edit = '$result->' . $field_id;
        }
        return  '<span style=" clear:both; float:left; padding:5px;">
                <p><label style="float: left; text-align: right; margin-right: 5px; width: 135px;" for="' . $field_id . '">' . $label . $r . '</label>                                
                <input id="' . $field_id . '" type="text" name="' . $field_id . '" value="<?php echo ' . urldecode($add_edit) . ' ?>"  />
                <?php echo form_error(\'' . $field_id . '\',\'<div>\',\'</div>\'); ?>
                </p></span>';      
    }
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Display control Text
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$k              string
     |		$v              string
     |      $required       string
     |      $option         string						
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function get_FormText( $k, $v, $required, $option ){
        $add_edit = '';
        if( $option == 'add' ){
            $add_edit = 'set_value(\'' . $k . '\');';
        }else if( $option == 'edit' ){
            $add_edit = '$result->' . $k . ';';
        }
            
        return '<span style="clear:both;float:left;padding:5px;">
                <p ><label for="' . $k . '">' . $v . $required . '</label>                                
                <textarea id="' . $k . '" name="' . $k . '"><?php echo ' . $add_edit . ' ?></textarea>
                <?php echo form_error(\'' . $k . '\',\'<div>\',\'</div>\'); ?>
                </p></span>';
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Display control Image
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$k              string
     |		$v              string
     |      $option         string									
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function get_FormImage( $k, $v, $option ){
        $add_edit ='';
        $url = '';
        if( $option == 'add' ){
            $add_edit = 'set_value(\'' . $k . '\');';
        }else{
            $add_edit = '$result->' . $k . ';';
            $url = '<?php echo base_url() ?>application/assets/images/books/scale/<?php echo $result->' . $k . ';?>';
        }
        return '<span style="float: left; position: relative; left: 30px; top: -207px;">
						<img src="' . $url . '" id="' . $k . '_img" alt="image" /><br/>
						<div class="upload">
						<input id="uploader-' . $k . '1" type="file" name="uploader-' . $k . '1" />
						</div>
						<input type="hidden" value="<?php echo ' . $add_edit . ' ?>" name="' . $k . '" id="' . $k . '" />
						<script type="text/javascript">
           $("#uploader-' . $k . '1").uploadify({
            "uploader"  : "<?php echo base_url()?>application/assets/js/uploadify/uploadify.swf",
            "script"    : "<?php echo base_url() ?>application/assets/js/uploadify/uploadify.php",
            "cancelImg" : "<?php echo base_url() ?>application/assets/js/uploadify/cancel.png",
            "folder"    : "/images/books",
            "auto"      : true,
            "removeCompleted" : true,
            "fileDesc"    : "Image File",
            "fileExt"     : "*.jpg;*.gif;*.png",            
            "multi"     : false,
            "onComplete"  : function(event, ID, fileObj, response, data) {             
				$("#' . $k . '").val(response);
				$("#' . $k . '_img").attr("src","<?php echo base_url() ?>application/assets/images/books/scale/"+response);
            }
        });
	</script></span>';
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Display control Checkbox
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$k              string
     |		$v              string
     |      $option         string								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function get_FormCheckBox( $k, $v, $option ){
        $add_edit = '';
        if( $option == 'add' ){
            $add_edit = 'FALSE';
        }else{
            $add_edit = '$result->' . $k;
        }
        return '<span style="clear:both;float:left;padding:5px; margin-left:130px;">
						<?php 
						$data = array(
							"name"        => "' . $k . '",
							"id"          => "' . $k . '",
							"value"       => 1,
							"checked"     => ' . $add_edit . ',
							"style"       => "margin:10px",
						);
						echo form_checkbox($data); ?> <label for="' . $k . '">' . $v . '</label></span>';
    }
    
    function get_FormRadio( $k, $v, $option ){
        $add_edit = '';
        if( $option == 'add' ){
            $add_edit = 'FALSE';
        }else{
            $add_edit = '$result->' . $k;
        }

        return '<span style="clear:both;float:left;padding:5px; margin-left:130px;">
						<?php 
						$data = array(
							"name"        => "' . $k . '",
							"id"          => "' . $k . '",
							"value"       => "' . urldecode($v) . '",
							"checked"     => ' . $add_edit . ',
							"style"       => "margin:10px",
						);
						echo form_radio($data); ?> <label for="' . $k . '">' . urldecode($v) . '</label></span>';
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Display control Dropdown
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$k              string
     |		$v              string
     |      $required       string
     |      $option         string								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
    function get_FormDefault( $k, $v, $required, $option ){
        $add_edit = '';
        if( $option == 'add' ){
            $add_edit = '';
        }else{
            $add_edit = ',$result->' . $k;
        }
        return '<span style=" clear:both; float:left; padding:5px;">
                <p><label for="' . $k . '">' . $v . $required . '</label>                                
                <?php
                enum = array(' . $this->input->post( $k . 'default' ) . '); 
                echo form_dropdown(\'' . $k . '\', $enum'. $add_edit . ');?>
                <?php echo form_error(\''.$k.'\',\'<div>\',\'</div>\'); ?>
                </p></span>';  
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Display control Input
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		$k              string
     |		$v              string
     |      $type           string
     |      $required       string
     |      $option         string								
     | - Return			: 
     |			- 
    \* ------------------------------------------------------------ */
     function get_FormInput( $field_id, $label, $required, $option ){
        $add_edit ='';
        if( $option == 'add' ){
            $add_edit = 'set_value(\'' . $field_id . '\');';
        }else{
            $add_edit = '$result->' . $field_id;
        }
        return  '<span style=" clear:both; float:left; padding:5px;">
                <p><label style="float: left; text-align: right; margin-right: 5px; width: 135px;" for="' . $field_id . '">' . $label . $required . '</label>                                
                <input id="' . $field_id . '" type="text" name="' . $field_id . '" value="<?php echo ' . $add_edit . ' ?>"  />
                <?php echo form_error(\'' . $field_id . '\',\'<div>\',\'</div>\'); ?>
                </p></span>';        
     }
     


?>